package org.sippr.controller;

import org.sippr.pojo.Result;
import org.sippr.pojo.Server;
import org.sippr.service.ServerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/server")
public class ServerController {
    @Autowired
    ServerService serverService;

    //增加服务器
    @PostMapping
    public Result add(@RequestBody @Validated Server value) {
        Server server = serverService.findByUrl(value.getAddress());
        if (server != null) {
            return Result.error("改地址已存在！");
        }
        serverService.add(value);
        return Result.success();
    }

    //获取服务器
    @GetMapping
    public Result list() {
        List<Server> serverList = serverService.list();
        return Result.success(serverList);
    }
    //删除服务器

    @DeleteMapping
    public Result delete(Integer id) {
        serverService.delete(id);
        return Result.success();

    }
}
